package com.ruoyi.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.biz.domain.BizProject;
import com.ruoyi.biz.mapper.BizProjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.biz.domain.BizArea;
import com.ruoyi.biz.mapper.BizAreaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 地区管理Service业务层处理
 *
 * @author tianl
 * @date 2024-12-19
 */
@Service
public class BizAreaService extends ServiceImpl<BizAreaMapper, BizArea> {
    @Autowired
    private BizProjectMapper bizProjectMapper;
//    public List<BizTree> selectTree(BizArea bizArea) {
//        List<BizArea> list = baseMapper.list(bizArea);
//        List<BizArea> parentList = list.stream().filter(item -> item.getPid() == 0).collect(Collectors.toList());
//        List<BizTree> resultList = new ArrayList<>();
//        for (BizArea parent : parentList) {
//            long count = packChildren(parent, bizArea.getName(), list);
//            if (StringUtils.isNotBlank(bizArea.getName())) {
//                if (count > 0 || parent.getName().contains(bizArea.getName())) {
//                    resultList.add(parent);
//                }
//            } else {
//                resultList.add(parent);
//            }
//        }
//        return resultList;
//    }
//
//    private long packChildren(BizArea parent, String keyword, List<BizArea> all) {
//        long count = 0;
//        if (!CollectionUtils.isEmpty(all)) {
//            List<BizArea> childList = all.stream().filter(item -> Objects.equals(item.getPid(), parent.getId())).collect(Collectors.toList());
//            List<BizTree> resultList = new ArrayList<>();
//            for (BizArea child : childList) {
//                long childCount = packChildren(child, keyword, all);
//                if (StringUtils.isNotBlank(keyword)) {
//                    if (childCount > 0 || child.getName().contains(keyword)) {
//                        resultList.add(child);
//                    }
//                } else {
//                    resultList.add(child);
//                }
//            }
//            count = resultList.size();
//            parent.setChildren(resultList);
//        }
//        return count;
//    }

    /**
     * 分页查询地区管理
     */
    public IPage<BizArea> listPage(BizArea req) {
        return baseMapper.list(PageUtils.page(), req);
    }

    /**
     * 查询地区管理列表
     */
    public List<BizArea> list(BizArea req) {
        return baseMapper.list(req);
    }

    /**
     * 根据ID查询
     */
    public BizArea selectById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 新增修改地区管理
     */
    @Transactional(rollbackFor = Exception.class)
    public void beforeSave(BizArea bizArea) {
        if (bizArea.getId() != null) {
            bizArea.setUpdateTime(DateUtils.getNowDate());
        } else {
            bizArea.setCreateTime(DateUtils.getNowDate());
            bizArea.setUpdateTime(DateUtils.getNowDate());
        }
        saveOrUpdate(bizArea);
    }

    /**
     * 批量删除地区管理
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            BizArea model = selectById(id);
            LambdaQueryWrapper<BizProject> wrapper = new LambdaQueryWrapper<>();
            wrapper.apply("JSON_CONTAINS(location, '\"" + model.getName() + "\"') OR JSON_CONTAINS(principal_location, '\" "+ model.getName() + "\"')");
            List<BizProject> projectList = bizProjectMapper.selectList(wrapper);
            if (!CollectionUtils.isEmpty(projectList)) {
                List<String> pName = projectList.stream().map(BizProject::getProjectName).collect(Collectors.toList());
                throw new ServiceException(model.getName() + "被项目[" + String.join(",", pName) + "]使用，不允许删除！");
            }
        }
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

}
